﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Globalization;
using System.Data;

namespace t01crm.Data
{
    public class DateValidationRule : ValidationRule
    {

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = ValidationResult.ValidResult;
            BindingGroup bindingGroup = (BindingGroup)value;
            Listing listing = (Listing)bindingGroup.Items[0];

            object oDateListed, oDateUpdated;
            DateTime dateListed, dateUpdated;
            var result1 = bindingGroup.TryGetValue(listing, "DatePosted", out oDateListed);
            var result2 = bindingGroup.TryGetValue(listing, "DateLastUpdated", out oDateUpdated);

            if (result1 && result2)
            {
                dateListed = (DateTime)oDateListed;
                dateUpdated = (DateTime)oDateUpdated;

                if (dateListed > dateUpdated)
                {
                    result = new ValidationResult(false, "Date Updated may not be before Date Listed.");
                }
            }

            return result;
        }


        /* public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = ValidationResult.ValidResult;
            BindingGroup bindingGroup = (BindingGroup)value;
            DataRowView listing = (DataRowView)bindingGroup.Items[0];

            object oDateListed, oDateUpdated;
            DateTime dateListed, dateUpdated;
            var result1 = bindingGroup.TryGetValue(listing, "DatePosted", out oDateListed);
            var result2 = bindingGroup.TryGetValue(listing, "DateLastUpdated", out oDateUpdated);

            if (result1 && result2)
            {
                dateListed = (DateTime)oDateListed;
                dateUpdated = (DateTime)oDateUpdated;

                if (dateListed > dateUpdated)
                {
                    result = new ValidationResult(false, "Date Updated may not be before Date Listed.");
                }
            }

            return result;
        }*/ 
    }
}
